﻿using System;
using Pap.Web.Modules.Membership.ViewModel;
using Pap.Web.Utils;
using Coolite.Ext.Web;
using System.Collections;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace Pap.Web.Modules.Membership.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class AssociationsLinks : BasePage<AssociationsLinksViewModel> //,IPermittedPage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ViewModel.CreateURLToNewCommittees();
                FillCommitteesURLs();
            }
        }

        /// <summary>
        /// Fills the committees UR ls.
        /// </summary>
        private void FillCommitteesURLs()
        {

            this.storeCommitteesURLs.DataSource = this.ViewModel.GetAllCommitteesURLs();
            this.storeCommitteesURLs.DataBind();
        }


        /// <summary>
        /// Handles the Submit event of the Btn control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void Btn_Submit(object sender, AjaxEventArgs e)
        {

            try
            {
                string jsonString = e.ExtraParams["gvCommitteesURLs"];
                JArray jsonArray = JSON.Deserialize(jsonString) as JArray;

                List<Guid> committeeIDs = new List<Guid>();
                List<string> CommitteeURLs = new List<string>();

                for (int i = 0; i < jsonArray.Count; i++)
                {
                    committeeIDs.Add(jsonArray[i]["CommitteeID"].ToString().Trim('"').ToGuid());
                    if (jsonArray[i]["CommitteeURL"].ToString().Trim('"') != "null")
                        CommitteeURLs.Add(jsonArray[i]["CommitteeURL"].ToString().Trim('"'));
                    else
                        CommitteeURLs.Add("");

                }

                this.ViewModel.UpdateCommitteesURLs(committeeIDs, CommitteeURLs);
            }
            catch (Exception exp)
            {
                string str = exp.Message;

            }
        }

        /// <summary>
        /// Handles the Click event of the btnBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnBack_Click(object sender, AjaxEventArgs e)
        {
            this.RedirectToPreviousPage();
        }
        //#region IPermittedPage Members

        //public string PermissionName
        //{
        //    get { return "AssociationsURLs"; }
        //}

        //#endregion
    }
}